package com.ctgu.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ctgu.springboot.common.QueryPageParam;
import com.ctgu.springboot.common.Result;
import com.ctgu.springboot.entity.Teacher;
import com.ctgu.springboot.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzs
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //新增老师
    @PostMapping("/save")
    public Result save(@RequestBody Teacher teacher) {
        if(StringUtils.isBlank(teacher.getTeaPwd())||"null".equals(teacher.getTeaPwd())){
            teacher.setTeaPwd("123456");
        }
        return teacherService.save(teacher) ? Result.suc() : Result.fail();
    }

    //更新老师信息
    @PostMapping("/update")
    public Result update(@RequestBody Teacher teacher) {
        return teacherService.updateById(teacher) ? Result.suc() : Result.fail();
    }


    //删除老师（通过ID）
    @GetMapping("/del")
    public Result del(@RequestParam String tea_id) {
        return teacherService.removeById(tea_id) ? Result.suc() : Result.fail();
    }
    //


    //分页查询老师信息
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        IPage<Teacher> result = teacherService.teacherPage(query);
        return Result.suc(result.getRecords(), result.getTotal());
    }

    //@GetMapping("list")
    //public List<Teacher> list(){
    //    return teacherService.list();
    //}
    //

    //@PostMapping("getById")
    //public Teacher getById(@RequestParam String id){
    //    return teacherService.getById(id);
    //}

    @RequestMapping("login")//教师登录
    public Result getStuInfoByPassword(String tea_phone, String tea_pwd){
        QueryWrapper<Teacher> teacherQueryWrapper=new QueryWrapper<>();
        teacherQueryWrapper.eq("tea_phone",tea_phone)
                .eq("tea_pwd",tea_pwd);
        if(teacherService.getOne(teacherQueryWrapper)!=null){
            Map<String,Object> resultMap = new HashMap<>();
            Teacher teacher=teacherService.getOne(teacherQueryWrapper);
            String token = UUID.randomUUID().toString();
            resultMap.put("teacher", teacher);
            resultMap.put("token",token);
            return Result.suc(resultMap);
        }else{
            return Result.fail();
        }
    }

    @RequestMapping("list")//教师企业id查找
    public List<Teacher> list(int com_id) {
        QueryWrapper<Teacher> teacherQueryWrapper=new QueryWrapper<>();
        teacherQueryWrapper.eq("com_id",com_id);//""中使用为数据库命名
        return teacherService.list(teacherQueryWrapper);
    }

    //获取所有老师信息
    @GetMapping("getList")
    public List<Teacher> getList(){
        return teacherService.getList();
    }



}
